package com.polaris.live.ui.party.fragment.roomInfo

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.adapter.party.RoomInfoFollowAdapter
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.FragmentFollowRoomInfoBinding
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.live.RoomInfoViewModel
import com.polaris.live.translation.R

/**
 * RoomInfoFollowMemberFragment
 * @author Created by 天晴 on 2024/10/16 14:42
 * @since 1.0.0
 **/
class RoomInfoFollowMemberFragment : BaseVbFragment<RoomInfoViewModel, FragmentFollowRoomInfoBinding>() {

    private val mUserId by lazy {
        arguments?.getLong("userId", 0) ?: 0
    }
    private val mAdapter: RoomInfoFollowAdapter by lazy {
        RoomInfoFollowAdapter()
    }

    private var onUserClick: ((userId: Long) -> Unit)? = null

    private var counts = 0L
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerView.setEnableRefresh(false)
        mBinding.recyclerView.setEmptyTip(
            com.polaris.live.R.drawable.salalogo,
            StringUtils.getString(R.string.no_focus),
            ColorUtils.getColor(com.polaris.live.R.color.roomInfoCommonTextColor)
        )
        mBinding.recyclerView.apply {
            recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            recycler.itemAnimator = null
            attachedAdapter(mAdapter, mViewModel).observe(
                this@RoomInfoFollowMemberFragment,
                mViewModel.fansLiveData
            )
        }
        mViewModel.setUserId(mUserId)
        mViewModel.onRefresh()

        mBinding.followersCount.text = "${StringUtils.getString(R.string.followers_people)}:$counts"

        mAdapter.setOnItemClickListener { _, view, position ->
            mAdapter.getItemOrNull(position)?.apply {
                Router.builder(RoutePath.USER_SPACE_ACTIVITY)
                    .withInt("page", UserConst.From.LIVE)
                    .withLong("userId", userId)
                    .withAny("userBasic", this)
                    .navigation(context)
            }
        }
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                com.polaris.live.R.id.av_fv -> {
                    onUserClick?.let { click ->
                        mAdapter.getItemOrNull(position)?.let {
                            click.invoke(it.userId)
                        }
                    }
                }
            }
        }
    }

    fun setCount(count: Long) {
        this.counts = count
        withLoaded {
            mBinding.followersCount.text = "${StringUtils.getString(R.string.followers_people)}:$count"
        }

    }

    fun setListener(onUserClick: (userId: Long) -> Unit) {
        this.onUserClick = onUserClick

    }
}